#!/usr/bin/lua -f
s=arg[0] while strsub(s,-1)~="/" do s=strsub(s,1,-2) end dofile(s.."common.lua")
function oneFile(file)
	if file then
		ret , rc = readfrom(file)
		if ( not ret ) then
			write ( "\n" .. "ERROR: file: "..file.." GOT: "..rc.."\n\n" )
			return
		end
	else
		file = "standard input"
	end
	if headers then
		print ( "==> " .. file .. " <==" );
	end
	if optc then
		write( read( optc ) )
	else
		for i = 1, optn do
			print( read("*l") )
		end
	end
	if headers then
		print()
	end
end
opts,narg,myst=getopts({c=1,n=1,q=0,v=0})
optc = opts["c"]
optn = opts["n"]
if opts["v"] then
	headers=1
end	
if not optc and not optn then
	if myst then
		last = strsub (myst,-1)
		if last=="b" or last=="k" or last=="m" then
			optc = myst
		else
			optn = myst
		end
	else
		optn = 10
	end
end
if optc then
	optc=bkm(optc)
end
if arg[narg] then
	if arg[narg + 1] then
		if not opts["q"] then
			headers=1
		end
	end
	repeat
		oneFile(arg[narg])
	narg = next(arg, narg)
	until narg == "n"
else
	oneFile()
end
